package com.meshnetics.gb.stack.core.config;

import com.meshnetics.gb.stack.core.IBParameter;
import com.meshnetics.gb.stack.core.InformationBase;
import com.meshnetics.gb.stack.core.InformationBaseImpl;

import java.util.Map;
import java.util.HashMap;
import java.util.Collections;

/**
 * Profile for the stack and it's components.
 *
 * @author mmarkov
 */
public class StackProfile {
    /** The name of this profile. */
    protected String name;

    /** Map of layer's InformationBase-s in this profile. */
    protected final Map ibs = Collections.synchronizedMap(new HashMap());

    /**
     * Constructs StackProfile having the given name.
     *
     * @param name the name for StackProfile
     */
    public StackProfile(String name) {
        this.name = name;
    }

    /**
     * Returns the name of this StackProfile.
     *
     * @return the name of this StackProfile
     */
    public String getName() {
        return name;
    }

    /**
     * Adds the parameter for StackLayer.
     *
     * @param layerName StackLayer name
     * @param param IBParameter to be added
     */
    public void addParameter(String layerName, IBParameter param) {
        InformationBase ib;

        synchronized (ibs) {
            ib = (InformationBase) ibs.get(layerName);

            if (ib == null) {
                ib = new InformationBaseImpl();
                ibs.put(layerName, ib);
            }
        }
        ib.addParameter(param);
    }

    /**
     * Returns the parameter for StackLayer.
     *
     * @param layerName StackLayer name
     * @param paramName IBParameter name
     * @return the parameter for StackLayer or null if none found
     */
    public IBParameter getParameter(String layerName, String paramName) {
        InformationBase ib = (InformationBase) ibs.get(layerName);
        return ((ib == null) ? null : ib.getParameter(paramName));
    }

    /**
     * Returns a copy of InformationBase for StackLayer with the given name.
     *
     * @param layerName StackLayer name
     * @return a copy InformationBase for StackLayer with the given name or null if none found
     */
    public InformationBase getInformationBase(String layerName) {
        InformationBase ib = (InformationBase) ibs.get(layerName);
        return ((ib == null) ? null : ((InformationBase) ib.clone()));
    }
}
